
import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
import { hashHistory } from 'dva/router'
import { getStatusUpDate,queryAllAddress,getArea,refreshOrder,updateStatus} from './popScreenAdListServ.js'
import { makeAddress } from "utils/util"

const defultState = {
  biz:{

  },
  areaList:[],
  area:[]
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({type: 'updateStore', payload: {}})
          // dispatch({type: 'getAreaList',payload:{}})
          // dispatch({type: 'getArea',payload:{}})
        }
      })
    },
  },


  effects: {
      //更新上下架状态
      *changStatus({ payload }, { put, call, select }) {
        console.log(payload)
        try{
           console.log(payload,'payload')
           const { rows, success,total,pageNo } = yield call(updateStatus,payload);
           let defultState = yield select(state => state[tmpModule.namespace])
           if(success){
             let messagetxt = ''
             if(payload.status==1){
              messagetxt = '上架成功'
             }
             if(payload.status==2){
              messagetxt = '下架成功'
            }
              message.success(messagetxt,()=>{
                defultState.biz.refreshListDatas(true)
              })
           }
        }catch(e){
            console.error(e)
        }
    },
      
      //更新状态与删除
      *updateStatus({ payload }, { put, call, select }) {
        console.log(payload)
        try{
           console.log(payload,'payload')
           const { rows, success,total,pageNo } = yield call(getStatusUpDate,payload);
           let defultState = yield select(state => state[tmpModule.namespace])
           if(success){
              message.success('删除成功',()=>{
                defultState.biz.refreshListDatas(true)
              })
           }
        }catch(e){
            console.error(e)
        }
    },
    *getArea({ payload }, { put, call, select }){
      const { data, success } = yield call(getArea);
      if(success) {
        // if(biz.queryForm && biz.queryForm.regionId){
        //   data.map((item,index)=>{
        //     if(biz.queryForm && biz.queryForm.regionId && biz.queryForm.regionId==item.value)
        //     {   
        //         products=item.products
        //     }
        //   })
        // }
        console.log(data)
        yield put({ type: 'updateStore',payload:{area:data}})
      }
    },
    *refreshOrder({ payload }, { put, call, select }){
      let defultState = yield select(state => state[tmpModule.namespace])
      const { success } = yield call(refreshOrder,payload.advertisingId);
      if(success){
        message.success('刷新排位成功',()=>{
          defultState.biz.refreshListDatas(true)
        })
      }
    },
    *getAreaList({ payload }, { put, call, select }){
        const { data, success } = yield call(queryAllAddress);
        if(success) {
            let arr = []
            data.map((item,index)=>{
              arr.push(item.value)
            })
            let areaList = []
            
            let arr1 = [data[0]]
            let attr = [data[data.length-1]]
            let firtOpt = data[0];
            let lastOpt = data[data.length - 1];
            // 返回数据有北京\澳门需要特别处理
            if (firtOpt["value"] == "110100") {
              data[0] = {
                label: "北京",
                value: "110100",
                children: arr1,
              };
            }
    
            if (lastOpt["value"] == "820000") {
              data[data.length - 1] = {
                label: "澳门",
                value: "820000",
                children: attr,
              };
            }
            // //去掉第三级，
            // areaList = makeAddress(data,2)
            areaList = makeAddress(data,2)
            console.log(areaList);
            yield put({ type: 'updateStore',payload:{areaList}})
        }
      },
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
